package cn.kgc.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.kgc.base.SystemConstant;
import cn.kgc.entity.Users;
import cn.kgc.form.UsersUpdateForm;
import cn.kgc.grace.result.GraceJSONResult;
import cn.kgc.grace.result.ResponseStatusEnum;
import cn.kgc.interceptor.JwtInterceptor;
import cn.kgc.service.UsersService;
import cn.kgc.utils.PagedGridResult;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author wangyue
 * @version 1.0
 */
@RequestMapping("/user")
@Api(tags = "求职者/招聘者用户管理模块")
@Validated
@Slf4j
@RestController
@SuppressWarnings("all")
public class UserController {

    @Resource
    private UsersService usersService;

    @PostMapping("/detail2")
    @ApiOperation(value="查询用户信息")
    @SentinelResource(value = "userDetail")
    public GraceJSONResult detail2(String id){
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return GraceJSONResult.ok(usersService.getById(id));
    }

    @PostMapping("/bankend/userIdList")
    public GraceJSONResult userIdList(@RequestParam("userName") String userName){
        LambdaQueryWrapper<Users> queryWrapper=new LambdaQueryWrapper<>();
        //按照用户名模糊查询
        queryWrapper.likeRight(Users::getRealName,userName);
        //只查询用户的id
        queryWrapper.select(Users::getId);
        //查询用户列表返回信息(用户列表中的用户只有用户的id)
        return GraceJSONResult.ok(usersService.list(queryWrapper));
    }

    @PostMapping("/chanageUserToCand")
    @ApiOperation(value = "HR离职")
    public GraceJSONResult  chanageUserToCand(){
        //获得登录的用户信息
        String s = JwtInterceptor.userIds.get();
        Users users = usersService.getById(s);
        if(users.getRole() == SystemConstant.ROLE_RECRUITERS){
            users.setRole(SystemConstant.ROLE_JOB_SEEKERS);
            users.setHrInWhichCompanyId("");
            return GraceJSONResult.ok(usersService.updateById(users));
        }
        return GraceJSONResult.errorMsg("您不是HR,无法离职");
    }

    @PostMapping("/saas/hrList")
    @ApiOperation(value="分页查询用户列表")
    public PagedGridResult hrList(
            @RequestParam(defaultValue = "1") int pageIndex,
            @RequestParam(defaultValue = "10") int pageSize){
        //获得登录的用户信息
        String s = JwtInterceptor.userIds.get();
        Users users = usersService.getById(s);
        //如果用户是招聘者
        if(users.getRole() == SystemConstant.ROLE_RECRUITERS){
            //获得招聘者所在的公司的编号
            String hrInWhichCompanyId = users.getHrInWhichCompanyId();
            //分页查询招聘者所在的公司中所有的HR的信息
            return usersService.hrList(pageIndex,pageSize,hrInWhichCompanyId);
        }else{
            //如果用户不是招聘者,返回空的集合
            return new PagedGridResult();
        }
    }

    @PostMapping("/updateUserRole")
    @ApiOperation(value = "将用户改为招聘者")
    public GraceJSONResult updateUserRole(
            @RequestParam("commitUserId") String commitUserId,
            @RequestParam("companyId") String companyId){
        return GraceJSONResult.ok(usersService.updateUserRole(commitUserId,companyId));
    }

    @PostMapping("/hrDetail")
    @ApiOperation(value = "根据招聘者的id获得对应的公司的ID")
    public GraceJSONResult hrDetail(String id){
        LambdaQueryWrapper<Users> usersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        usersLambdaQueryWrapper.eq(Users::getId,id);
        usersLambdaQueryWrapper.eq(Users::getRole, SystemConstant.ROLE_RECRUITERS);
        Users users=usersService.getOne(usersLambdaQueryWrapper);
        if(users==null){
            //如果当前用户还不是招聘者,没有对应的公司的ID
           return GraceJSONResult.ok("");
        }
        //如果是招聘者,返回对应的公司的ID
        return GraceJSONResult.ok(users.getHrInWhichCompanyId());
    }


    @PostMapping("/modify")
    @ApiOperation(value = "求职者/招聘者修改自己的信息")
    public GraceJSONResult  modify(UsersUpdateForm usersUpdateForm){
        //1 需要修改的用户是否是登录用户,如果不是返回错误信息
        String userId = JwtInterceptor.userIds.get();
        if(!usersUpdateForm.getId().equals(userId)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_INFO_UPDATED_ERROR);
        }
        //2 修改用户信息
        Users users = usersService.getById(userId);
        BeanUtil.copyProperties(usersUpdateForm,users);
        return GraceJSONResult.ok(usersService.updateById(users));
    }

    @PostMapping("/detail")
    @ApiOperation(value = "求职者/招聘者查询用户信息")
    @SentinelResource(value="userDetail")
    public GraceJSONResult  detail(){
        String userId = JwtInterceptor.userIds.get();
        return GraceJSONResult.ok(usersService.getById(userId));
    }

    @PostMapping("/hrCountInCompany")
    @ApiOperation(value = "查询公司HR数量")
    public GraceJSONResult getHrCountInCompany(String companyId){
        return GraceJSONResult.ok(usersService.getHrCount(companyId));
    }
}
